package action;

import global.Constants;
import global.beanUtils.BeanUtils;
import global.security.SessionUtils;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.UserMetaService;
import util.ServletHelp;
import util.StringUtils;
import domain.UserMeta;

/**
 * 用户元数据Action类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class UserMetaAction extends BaseAction {
	/**
	 * 用户元数据Service
	 * */
	private UserMetaService userMetaService = null;

	/**
	 * @param userMetaService
	 *            the userMetaService to set
	 */
	public void setUserMetaService(UserMetaService userMetaService) {
		this.userMetaService = userMetaService;
	}

	/**
	 * 获得笔记分类树
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward getFavoriteMenuTree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JSONArray res = userMetaService.getFavoriteMenuTree();
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 保存用户元数据
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数设定
		Map map = request.getParameterMap();
		UserMeta userMeta = new UserMeta();
		BeanUtils.populate(userMeta, map);
		userMeta.setUserId(SessionUtils.getCurrentUserId());
		userMeta.setStatus(Constants.STATUS_1);

		String userMetaId = request.getParameter("userMetaId");
		// 保存
		if (StringUtils.isNotEmpty(userMetaId)) {
			// 更新用户元数据
			userMetaService.update(userMeta);
		} else {
			// 添加用户元数据
			userMetaService.insert(userMeta);
		}

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
	
	/**
	 * 添加快捷菜单
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward addFavoriteMenu(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数设定
		Map map = request.getParameterMap();
		UserMeta userMeta = new UserMeta();
		BeanUtils.populate(userMeta, map);
		userMeta.setUserId(SessionUtils.getCurrentUserId());
		userMeta.setStatus(Constants.STATUS_1);

		// 添加用户元数据
		userMetaService.addFavoriteMenu(userMeta);

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
	
	/**
	 * 删除用户元数据
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 获得用户元数据ID
		String userMetaId = request.getParameter("userMetaId");
		if (StringUtils.isNotEmpty(userMetaId)) {
			// 删除用户元数据
			userMetaService.delete(Integer.parseInt(userMetaId));
		}
		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
}
